package com.tianqu.tms.core.mail;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

import com.tianqu.tms.core.base.Logger;

/**
 * @author liugang
 *
 */
public class MailHelperDigester {
	private Logger logger = Logger.getLogger(MailHelperDigester.class);

	public MailHelperDigester() {
		;
	}

	/**
	 * <p>读取mail的xml文件配置文件，并生成MailHelperManager对象</p>
	 * 
	 * @param fileName
	 * @return MailHelperManager
	 * @throws java.io.IOException
	 * @throws org.xml.sax.SAXException
	 * @throws javax.xml.parsers.ParserConfigurationException
	 */
	public MailHelperManager digest( String fileName )
									throws ParserConfigurationException,
											SAXException,
											IOException {

		MailHelperManager helperManager = null; 
		InputStream  in       		 	= null; 

		try {

			// 配置文件的解析。
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();
			Digester digester = new Digester( parser );

			// MailHelperManager的生成
			digester.addObjectCreate( "mailhelpers", MailHelperManager.class );
			digester.addSetProperties( "mailhelpers" );

			// MailHelper的生成（一级菜单）
			digester.addObjectCreate(
				"mailhelpers/mailhelper"
				,MailHelper.class
				);
			// 保存MailHelper的上一级到MailHelperManager（MailHelper→MailHelperManager）
			digester.addSetNext(
				"mailhelpers/mailhelper"
				,"add"
				);
			digester.addSetProperties("mailhelpers/mailhelper" );
			
			digester.addBeanPropertySetter("mailhelpers/mailhelper/title","templateTitle");
			
			digester.addBeanPropertySetter("mailhelpers/mailhelper/body","templateBody");
			in = new FileInputStream( fileName );
			helperManager = (MailHelperManager)digester.parse( in );

		} catch ( Exception e ) {
			logger.error(e);

		} finally {
			if ( in != null ) {
				try {
					in.close();
				} catch ( IOException ioe ) {
					;
				}
			}
		}
		return helperManager;
	}
	
//	public static void main(String[] args){
//		MailHelperDigester digester = new MailHelperDigester();
//		try{
//			MailHelperManager manager = digester.digest("d:/mailhelper.xml");
//			//System.out.println(manager.getAppName()+""+manager.getAppUrl());
//			//System.out.print(manager.getChildrenMap().size());
//			/*Map children = manager.getChildrenMap();
//			Iterator it = children.keySet().iterator();
//			while(it.hasNext()){
//				String id = (String)it.next();
//				MailHelper helper = (MailHelper)children.get(id);
//				System.out.println(helper.getTemplateTitle()+"\n"+helper.getTemplateBody());
//				System.out.println("\n\n");
//			}*/
////			String temp = manager.makeMailTitle("0012","12345678");
////	//		System.out.print(temp);
////	//		System.out.println();
////			temp = manager.makeMailBody("0012","12345678");
//	//		System.out.print(temp);
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//	}
}
